<?php
/**
 * Created by PhpStorm.
 * User: 图图
 * Date: 2021-6-8
 * Time: 16:29
 */

namespace app\web\controller;

use app\common\controller\Api;
use app\common\model\Factor;
use app\common\model\Fenorder;
use app\common\model\Goodscat;
use app\common\model\Nav as NavModel;
use think\Db;

/**
 * 前台首页控制器
 * @package app\index\controller
 */
class Nav extends Home
{
    public $noNeedLogin = ['*'];

    /**
     * 获取商品所有的一级导航
     */
    public function get_nav(){
        $data = Goodscat::where('pid',0)->field('id,name,sort')->order('sort asc')->select();
        $this->result('请求成功',$data,200);
    }

    /**
     * 获取商品分类列表
     */
    public function lists(){
        if($this->request->isGet()){
            $getData= $this->request->get();
            if(empty($getData['page'])){
                $getData['page'] = 1;
            }
            //按上级栏目查找下级分类
            if(isset($getData['nav_id'])){
                if(empty($getData['nav_id'])){
                    $this->result('无效的参数','',204);
                }
                $where['pid'] = $getData['nav_id'];
                $navData = Goodscat::where($where)->field('id,name,logo')->order('sort asc')->paginate();
            }else{
                $navData = Goodscat::where('pid','<>',0)->field('id,name,logo')->order('sort asc')->paginate();
            }
            foreach ($navData as $key =>$value){
                $imgUrl = get_img_path($value['logo']);
                $navData[$key]['logo'] = $imgUrl;
            }
            $this->result('查询成功',$navData,200);

        }
    }



}
